// MAC取得

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>

int main(int argc, char argv[])
{
    struct ifreq myifreq;
    int sock;
    char mac[50];

    sock = socket(AF_INET, SOCK_STREAM, 0);
    strcpy(myifreq.ifr_name, "eth0");
    ioctl(sock, SIOCGIFHWADDR,&myifreq);

    sprintf(mac, 
            "%X:%X:%X:%X:%X:%X",
            (unsigned char)myifreq.ifr_hwaddr.sa_data[0],
            (unsigned char)myifreq.ifr_hwaddr.sa_data[1],
            (unsigned char)myifreq.ifr_hwaddr.sa_data[2],
            (unsigned char)myifreq.ifr_hwaddr.sa_data[3],
            (unsigned char)myifreq.ifr_hwaddr.sa_data[4],
            (unsigned char)myifreq.ifr_hwaddr.sa_data[5] );

    printf("MAC: %s\n", mac);

    getchar();
}
